# -*- coding:utf-8 -*-
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
class Solution:
    def Serialize(self, root):
        serializeStr = ''
        if root == None:
            return '#'
        stack = []
        while root or stack:
            while root:
                serializeStr += str(root.val) + ','
                stack.append(root)
                root = root.left
            serializeStr += '#,'
            root = stack.pop()
            root = root.right
        serializeStr = serializeStr[:-1]
        return serializeStr

    def Deserialize(self, s):
        # write code here
        l = s.split(',')
        t=self.help(l)
        return t
    def help(self, l):
        if len(l) == 0:
            return None
        val = l.pop(0)
        if val != '#':
            root = TreeNode(int(val))
            root.left = self.help(l)
            root.right = self.help(l)
            return root
        else:
            return None


pNode1 = TreeNode(8)
pNode2 = TreeNode(6)
pNode3 = TreeNode(10)


pNode1.left = pNode2
pNode1.right = pNode3


S = Solution()
newList = S.Serialize(pNode1)
print(newList)
a= S.Deserialize('8,6,#,#,10')
print(a)